package com.hoshiicloud.shxping.feign;

import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.shxping.dto.UploadFileResponse;
import com.hoshiicloud.shxping.feign.hystrix.HystrixFileServerFeignFallback;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author yangcheng
 * @version 1.0
 * @title: IFileServerFeignClient
 * @description: 文件服务FeignClient
 * @date 2019/8/5 15:33
 * @mail maiboyc@163.com
 */
@FeignClient(name = "hoshiicloud-fileserver", configuration = IFileServerFeignClient.MultipartSupportConfig.class, fallback = HystrixFileServerFeignFallback.class)
public interface IFileServerFeignClient {

    /**
     * 上传文件
     * @param bucketName 存储空间名称，上传时指定的存储空间
     * @param objectKey 文件key,上传时存储的对象key，上传完毕后原样返回，如不传则使用文件MD5值。
     * @param fileName 文件名,批量上传时需和key对应
     * @param folder 文件夹路径
     * @param privateOwned 对象私有读写
     * @param file 文件
     * @return Response
     */
    @RequestMapping(value = "/file/uploadFile/", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    Response<UploadFileResponse> uploadFile(@RequestParam("bucketName") String bucketName, @RequestParam("objectKey") String objectKey, @RequestParam("fileName") String fileName, @RequestParam("folder") String folder, @RequestParam("privateOwned") Boolean privateOwned , @RequestPart("file") MultipartFile file);

    @RequestMapping(method = RequestMethod.POST, value = "/file/uploadOneFile",
            produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    Response uploadFile(@RequestPart(value = "file") MultipartFile file, @RequestParam(value = "bucketName") String bucketName, @RequestParam("objectKey") String objectKey, @RequestParam("fileName") String fileName, @RequestParam("folder") String folder, @RequestParam("privateOwned") Boolean privateOwned);



class MultipartSupportConfig {

        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;

        @Bean
        public Encoder feignEncoder() {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }
    }
}
